<template>
	<view class="wrap">
		<view class="order_detail">
			<view class="top">
				<view class="address_detail">
					{{order.consigneeAddress}}
					<br>
				</view>
				<view class="footer_btn">
					<u-button style="margin-right: 0.4rem;" size="mini" type="info" @click="contact"> 联系客服</u-button>
					<u-button style="margin-right: 0.4rem;" size="mini" type="primary" @click="payment" v-show="order.statusName=='待付款'">立即付款
					</u-button>
					<u-button style="margin-right: 0.4rem;" size="mini" type="success" @click="confirmReceive" v-show="order.statusName=='已发货'">确认收货
					</u-button>
				</view>
			</view>
			<view class="content">
				<view class="title">
					{{'订单编号: ' + order.orderSn}}
				</view>
				<view class="list">
					<view class="item" v-for="(item,index) in order.items" :key="index">
						<u-row gutter="16">

							<u-col :span="3">
								<u-image width="100%" height="150rpx" :src="imgUrl+item.goods.pic"></u-image>
							</u-col>
							<u-col :span="9" @click="toGoods(item.goods.id)">
								<view class="gl-name">{{item.goods.name}}</view>
								<view class="gl-descript">{{item.goods.descript}}</view>
								<view class="gl-price">
									<u-row>
										<u-col offset="6" :span="3" text-align="right">
											￥{{formatPrice(item.price)}}
										</u-col>
										<u-col :span="3" text-align="right">
											x{{item.count}}
										</u-col>
									</u-row>
								</view>

							</u-col>
						</u-row>
						<u-row gutter="16" style="margin-top: 0.5rem;display:inline-block;">

							<u-col v-show="getRefundBtnShow(item)" :span="3">
								<view style="margin-right: 0.4rem;">
									<u-button size="mini" type="info" @click.stop="refundOrder(item)">申请退款
									</u-button>
								</view>

							</u-col>

							<u-col v-show="getRefundCancelBtnShow(item)" :span="3">

								<view style="margin-right: 0.4rem;">
									<u-button size="mini" type="info" @click.stop="refundOrderCancel(item)">取消退款
									</u-button>
								</view>

							</u-col>
							<u-col v-show="getRefundInfoShow(item)" :span="3">
								<view style="margin-right: 0.4rem;">
									<u-button size="mini" type="info">退款处理中</u-button>
								</view>
							</u-col>
							
							<u-col v-show="getRefundAllShow(item)" :span="3">
								<view style="margin-right: 0.4rem;">
									<u-button size="mini" type="info">已全部退款</u-button>
								</view>
							</u-col>
							

							
						</u-row>
					</view>
				</view>
				<view class="total">合计: {{formatPrice(order.totalPrice)}}</view>

			</view>
			<view>
				<u-cell-group title="订单信息">
					<u-cell-item title="订单编号" :value="order.orderSn" :arrow="false"></u-cell-item>
					<u-cell-item title="创建时间" :value="order.createTime" :arrow="false"></u-cell-item>
				</u-cell-group>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: this.baseApi + '/file/getImgStream?idFile=',
				order: {
					orderSn: '',
					orderid:'',
					consigneeAddress: '',
					address: {
						name: ''
					}
				},
				refundOrderItem:{}
			}
		},
		onLoad(option) {
			this.order.orderSn = option.orderSn;
			this.order.id = option.orderid;
			this.getData()
		},
		methods: {
			getData() {
				this.$u.get('user/order/' + this.order.orderSn).then(res => {
					this.order = res
				});
				
				//获取退款订单状态
				this.$u.get("user/order/getOrderRefundItemStatus/"+ this.order.id).then( response => {
					
				   this.refundOrderItem = response;
				});
				
			},
			formatPrice(price) {
				return (price / 100).toFixed(2);
			},
			toGoods(id) {
				this.$u.route({
					url: '/pages/goods/goods',
					params: {
						id: id,
					}
				})
				
			},
			payment() {
				this.$u.route({
					url: '/pages1/order/payment/payment',
					params: {
						orderSn: this.order.orderSn,
						totalPrice: this.order.totalPrice
					}
				})

			},
			confirmReceive() {
				this.$u.post('user/order/confirm/' + this.order.orderSn).then(res => {
					this.order = res
				})
			},
			refundOrder(orderItem) {
				
				this.$u.route({
					url: '/pages1/order/refund/index',
					params: {
						orderSn: this.order.orderSn,
						itemid: orderItem.id
					}
				})
				
				// this.$router.push({
				// 	path: '/pages1/order/refund/index',
				// 	params: {
				// 		orderSn: this.order.orderSn,
				// 		itemid: orderItem.id
				// 	}
				// });
			},
			refundOrderCancel(orderItem) {

				let cancelRefundData = {
					orderSn: this.order.orderSn,
					itemid: orderItem.id
				}

				this.$u.post("/M0005", cancelRefundData).then(response => {
					this.$u.toast("取消退款成功");

					this.getData();
				}).catch(err => {

					this.getData();
					console.log('err', err)
					this.$u.toast(err)
				})

			},
			getOrderStatus() {

				if (this.order == undefined || this.order == null || this.order.status == 1 || this.order.status == 5 ||
					this.order.status == 6 || this.order.status == 7 || this.order.status == 8) {
					return false;
				}

				return true;
			},
			getRefundBtnShow(orderItem) {
				//如果已经全部退款则不在显示
				if(this.isRefundAll(orderItem)){
					return false;
				}
				//退款申请按钮
				if (!this.getOrderStatus()) {
					return false;
				}

				if (this.refundOrderItem == null || this.refundOrderItem[orderItem.id + ''] == undefined) {
					return true;
				} else {
					return false;
				}

			},
			getRefundInfoShow(orderItem) {

				//如果已经全部退款则不在显示
				if(this.isRefundAll(orderItem)){
					return false;
				}

				//退款进度按钮
				if (!this.getOrderStatus() || this.refundOrderItem == null) {
					return false;
				}

				if (this.refundOrderItem == null || this.refundOrderItem[orderItem.id + ''] == undefined) {
					return false;
				}

				if (this.refundOrderItem[orderItem.id + ''] == 1) {
					return false;
				} else {
					return true;
				}

			},

			getRefundCancelBtnShow(orderItem) {
				//如果已经全部退款则不在显示
				if(this.isRefundAll(orderItem)){
					return false;
				}
				
				//取消退款按钮

				if (!this.getOrderStatus() || this.refundOrderItem == null) {
					this.cancelBtnDisabled = false;
					return false;
				}

				if (this.refundOrderItem == null || this.refundOrderItem[orderItem.id + ''] == undefined) {
					return false;
				}

				if (this.refundOrderItem[orderItem.id + ''] == 1) {
					//退款申请中才可以取消
					return true;
				} else {
					return false;
				}
			},
			isRefundAll(orderItem){
				//退款金额等于总金额时
				if(orderItem.refundPrice==orderItem.totalPrice){
					return true;
				}
				return false;
			},
			//是否全部退款
			getRefundAllShow(orderItem){
				return this.isRefundAll(orderItem);
			},
			
			contact() {
				this.$u.toast('敬请期待')
			}

		}
	}
</script>

<style lang="scss" scoped>
	.order_detail {
		margin: 30rpx;

		.top {
			.address_detail {
				padding: 5px;
			}

			.footer_btn {
				text-align: right;

				.u-btn {
					margin-left: 20rpx;
				}
			}
		}

		.content {
			margin-top: 40rpx;

			.total {
				text-align: right;
				padding: 10px;
			}

			.list {
				.item {
					margin-top: 20rpx;

					.gl-name {
						font-size: 26rpx;
					}

					.gl-descript {
						margin-top: 14rpx;
						font-size: 20rpx;
					}

					.gl-price {
						margin-top: 26rpx;
						font-size: 24rpx;
					}
				}
			}

			.total {}
		}



	}
</style>
